package com.fnzx.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fnzx.entity.DescribeInfoEntity;
import com.fnzx.service.DescribeInfoService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.R;
import com.fnzx.util.oss.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-10-07 10:54:12
 */
@RestController
@RequestMapping("iot/describeinfo")
@Api(tags ="网站主体内容描述")
public class DescribeInfoController {
    @Autowired
    private DescribeInfoService describeInfoService;

    /**
     * 列表
     */
    @ApiOperation(value = "前台页面-获取网站中心介绍,7个页面依次1，2，3，4，5，6，7")
    @GetMapping({"/list","/info"})
    public R list(@RequestParam(required = false) Integer fromPage){
        List<DescribeInfoEntity> pageInfo = describeInfoService.getPageInfo(fromPage);
        return R.ok().put("page", pageInfo);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "后台页面--添加描述",notes = "{\n" +
            "\t\"content\": \"链接描述连接描述连接描述2222\",\n" +
            "\t\"fromPage\": 5,\n" +
            "\t\"parentId\": 5,\n" +
            "\t\"title\": \"客户体验\",\n" +
            "\t\"videoLink\": \"https://music.163.com/store/newalbum/detail?id=135521226\"\n" +
            "}")
    @PostMapping("/save")
    public R save(@RequestBody DescribeInfoEntity describeInfo,
                  @RequestHeader String token){
		describeInfoService.save(describeInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "后台页面--修改描述",notes = "{\"id\":,\"title\":,\"content\":,\"video_link:\":}")
    @PostMapping("/update")
    public R update(@RequestBody DescribeInfoEntity describeInfo,
                    @RequestHeader String token){
        Long id = describeInfo.getId();
        String videoLink = describeInfoService.getById(id).getVideoLink();
        if (!videoLink.equals(describeInfo.getVideoLink())) {
            OssUtils.delete(videoLink);
        }
        describeInfoService.updateById(describeInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "后台页面--删除描述",notes = "根据id删除描述[id1,id2,……]")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids,
                    @RequestHeader String token){
        for (Long id : ids) {
            String videoLink = describeInfoService.getById(id).getVideoLink();
            OssUtils.delete(videoLink);
        }
		describeInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    @ApiOperation(value = "修改内容",notes = "{\"id\":,\"childrenEntities\":{},\"title\":,\"content\":,\"video_link:\":}")
    @PostMapping("/updateCen")
    public R updateCen(@RequestBody DescribeInfoEntity describeInfoEntity,
                    @RequestHeader String token){
        describeInfoService.updateById(describeInfoEntity);
        return R.ok();
    }

    //上传文件（图片和视频）到阿里云oss
    @ApiOperation(value = "后台管理系统--上传视频", notes = "上传文件（图片和视频）到阿里云oss")
    @PostMapping("/videoUpload")
    public R fileUpload(MultipartFile file, @RequestHeader String token) throws IOException {
        //获取上传文件
        String url = OssUtils.upload(file);
        return R.ok(url);
    }

}
